package com.bsb.biz.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bsb.base.model.SysResource;
import com.bsb.base.model.SysResourceExample;
import com.bsb.biz.service.SysResourceService;



@Controller
@RequestMapping("/resource")
public class SysResourceController{
	
	
	private String redirect = "redirect:/resource";
	
	@Resource
	private SysResourceService sysResourceService;
	
	@ModelAttribute("types")
    public SysResource.ResourceType[] resourceTypes() {
        return SysResource.ResourceType.values();
    }
	
	@RequiresPermissions("resource:view")
	@RequestMapping(method = RequestMethod.GET)
	public String list(
			Model model) {
		SysResourceExample sysResourceExample = new SysResourceExample();
		sysResourceExample.setOrderByClause("concat(parent_ids, id) asc");
		List<SysResource> sysResources = sysResourceService.selectByExample(sysResourceExample);
		model.addAttribute("sysResources", sysResources);
		return "/SysResource/list";
	}
	
	@RequiresPermissions("resource:create")
	@RequestMapping(value = "/{parentId}/appendChild", method = RequestMethod.GET)
	public String showAppendChildForm(@PathVariable("parentId") Long parentId,Model model) {
		 SysResource parent = sysResourceService.getSysResourceById(parentId);
        model.addAttribute("parent", parent);
        SysResource child = new SysResource();
        child.setParent_id(parentId);
        child.setParent_ids(parent.makeSelfAsParentIds());
        model.addAttribute("resource", child);
        model.addAttribute("op", "新增子节点");
		return "/SysResource/edit";
	}

	@RequiresPermissions("resource:create")
	@RequestMapping(value = "/{parentId}/appendChild", method = RequestMethod.POST)
	public String add(Model model,RedirectAttributes redirectAttributes, @ModelAttribute("resource") SysResource sysResource) {
		sysResourceService.addSysResource(sysResource);
		redirectAttributes.addFlashAttribute("msg", "新增子节点成功");
		return redirect;
	}

	@RequiresPermissions("resource:delete")
	@RequestMapping("/{id}/delete")
	public String del(Model model,@PathVariable("id") Long id,RedirectAttributes redirectAttributes) {
		sysResourceService.delResource(id);
		redirectAttributes.addFlashAttribute("msg", "删除成功");
		return redirect;
	}

	@RequiresPermissions("resource:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String showUpdateForm(Model model,@PathVariable("id") Long id) {
		SysResource sysResource = sysResourceService.getSysResourceById(id);
		model.addAttribute("resource", sysResource);
		 model.addAttribute("op", "修改");
	        return "SysResource/edit";
	}

	@RequiresPermissions("resource:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String edit(Model model,RedirectAttributes redirectAttributes, @ModelAttribute("sysResource") SysResource sysResource) {
		sysResourceService.updateSysResource(sysResource);
		redirectAttributes.addFlashAttribute("msg", "修改成功");
		return redirect;
	}

	@RequiresPermissions("resource:view")
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String view(Model model,
			@RequestParam(value = "id", required = true) Long id) {
		SysResource sysResource = sysResourceService.getSysResourceById(id);
		model.addAttribute("sysResource", sysResource);
		return "/SysResource/view";
	}
}
